<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="author" content="featon inc.">
    <link rel="stylesheet" href="/static/vendor/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/vendor/bootstrap/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/static/vendor/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/vendor/skin/ftskin.css">
    <link rel="stylesheet" href="/static/vendor/webuploader/webuploader.css" type="text/css">
    <link rel="stylesheet" href="/static/vendor/datetimepicker/jquery.datetimepicker.css">
    <title></title>
</head>

<body style="margin-bottom: 60px;">

<div class="ft-subpage-wrapper container-fluid">
    <form id="form1" name="form1" action="#" method="post">
        <div class="row">
            <div class="col-lg-12" id="form_table">
                <fieldset>
                    <legend id="titleText">--</legend>
                    <table id="" class="form-table form-table-border" cellpadding="0" cellspacing="0" >
                        <tr>
                            <th><span class="f_red">*</span>姓名</th>
                            <td>
                                <input id="user_realname" name="user_realname" type="text" class="form-control" value="" placeholder="" >
                                <span class="cError"></span>
                            </td>
                            <th>用户类型</th>
                            <td>
                                <select id="user_type" name="user_type" class="form-control" disabled style="width:300px">
                                    <option value="1">业务人员</option>
                                </select>
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>所属部门</th>
                            <td>
                                <div class="input-group">
                                    <input id="dq_yw_id" name="dq_yw_id" type="hidden" value="0">
                                    <input id="dept_name" name="dept_name" type="text" class="form-control" value="" readOnly="readOnly" style="width:220px">
                                    <span class="input-group-btn  ft-input-group-btn">
							<button class="btn btn-default" type="button" onclick="openSelectSinglDiQuPage('选择部门','setDiQuID')" disabled> 选择部门</button>
						  </span>
                                </div>
                                <span class="cError"></span>
                            </td>
                            <th>电话</th>
                            <td>
                                <input id="user_phone" name="user_phone" type="text" class="form-control" value="" placeholder="" >
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th>照片</th>
                            <td colspan="3">
                                <div class="min-pic-box">
                                    <ul class="min-pic-ul clearfix" id="minPicBox_img">
                                    </ul>
                                </div>
                                <div class="form-group">
                                    <div style="margin:auto;" id="pic_button_img"> 上传照片</div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <th>描述</th>
                            <td colspan="3">
                                <textarea id="remark" name="remark" class="form-control" rows="2" ></textarea>
                            </td>
                        </tr>
                    </table>
                </fieldset>
                <div class="bottom-tools-box">
                    <button type="button" class="btn btn-info"  id="save_but"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> 保存</button>
                    <button type="button" class="btn btn-warning" onclick="top.tab_colseOnclick();"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span> 取消</button>
                </div>

            </div>
        </div>
    </form>
</div>
<input type="hidden" th:id="dept_id" th:value="${dept_id}" />
<input type="hidden" th:id="user_id" th:value="${user_id}" />
<script src="/static/vendor/jquery/jquery-3.3.1.min.js"></script>
<script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/vendor/validate/jquery.validate.min.js" type="text/javascript"></script>
<script src="/static/vendor/webuploader/webuploader.js" type="text/javascript"></script>
<script src="/static/vendor/skin/js/ft-ui.js"></script>
<script src="/static/vendor/skin/js/jquery.c.js"></script>
<script src="/static/vendor/skin/js/java.js"></script>
<script src="/static/vendor/skin/js/upload-util.js"></script>
<script src="/static/vendor/sortable/sortable.js" ></script>
<script src="/static/vendor/datetimepicker/jquery.datetimepicker.js"></script>
<script>
    var dept_id = $("#dept_id").val();
    var user_id = $("#user_id").val();
    var picUpHandl_img;
    var con_map = new Map();
    var defaultBean;
    //var picUpHandl;
    // var selections = document.getElementById("user_realname");
    jQuery(function($) {
        picUpHandl_img = new PublicUploadImage("pic_button_img",'minPicBox_img',true,false);

        iniInfoForm();
        if (null != user_id && user_id != "") {
            defaultBean = top.ajax.syncl("/user/getUserBean/"+user_id,"");
            if(defaultBean)
            {
                $("#form_table").autoFill(defaultBean);
                picUpHandl_img.setPicToBox(defaultBean.user_photo);
                $("#titleText").text("修改用户");
                $('#save_but').click(updateObject);

            }
        } else {
            $('#save_but').click(addObject);
            //获取部门所关联的地区对象,并设置为默认
            var dept_bean = top.ajax.syncl("/dept/getDeptBean/"+dept_id,"");
            if(dept_bean.dq_yw_id != 0)
                setDiQuID(dept_bean.dq_yw_id,dept_bean.dept_name);

            $("#titleText").text("添加用户");
            // showStaffList();
        }
    });

    $(document).ready(function(){

    });

    // 根据部门ID获取执法人员
    // function showStaffList(){
    //
    //     con_map.put("dept_id", dept_id);
    //
    //     beanList = top.ajax.synclBody("/sys/cs/project/system/staff/getStaffListByDeptId", con_map.toJSON());
    //
    //     if(null != beanList){
    //         $.each(beanList,function(i,bean){
    //             var option = document.createElement("option");
    //             option.value = bean.staff_id;
    //             option.text= bean.name;
    //             selections.options.add(option);
    //         });
    //
    //     }
    // }

    // 选择执法人员时触发
    // function checkStaff(){
    //     var staff_id = $("#user_realname").val();
    //     var staff_bean = top.ajax.syncl("/sys/cs/project/system/staff/getStaffBean/"+staff_id,"");
    //     $("#user_phone").val(staff_bean.phone);
    // }

    function iniInfoForm()
    {
        var validator = $("#form1").validate({
            debug:true,
            rules: {
                user_realname: {
                    required: true
                },
                dq_yw_name: {
                    required: true
                }
            },
            messages: {
                user_realname: {
                    required: "执法人员不能为空！"
                },
                dq_yw_name: {
                    required: "所属地区不能为空"
                }
            },
            errorPlacement: function(error, element) {
                error.appendTo( element.parents('td').find(".cError") );
                // error.appendTo(element.parent());
            },
            submitHandler: function(form) {

            }
        });
    }

    jQuery.validator.addMethod("accountExist", function(value, element){
        return !top.ajax.syncl("/sys/cs/org/user/registerISExist/"+value,"");
    }, "该帐号已存在，请重新输入帐号");

    var is_submit = false;
    function addObject()
    {
        if($("#form1").valid())
        {
            if(is_submit == false)
            {
                is_submit = true;
                var bean = top.ajax.getBean("com.yixun.entity.YxUser");
                $("#form_table").autoBind(bean);
                bean.dept_id = dept_id;
                bean.dq_id = $("#dq_yw_id").val();
                bean.user_photo = picUpHandl_img.getImgData();

                if(top.ajax.synclBody("/user/add",bean))
                {
                    modelWindow.alert(top.CMSLang.Add_success);
                    top.getCurrentFrameObj().loadListble();
                    top.tab_colseOnclick();
                }else
                {
                    modelWindow.error(top.CMSLang.Add_fail);
                    is_submit = false;
                }
            }
        }
    }

    function updateObject()
    {
        if($("#form1").valid())
        {
            if(is_submit == false)
            {
                is_submit = true;
                var bean = top.ajax.getBean("com.yixun.entity.YxUser");
                $("#form_table").autoBind(bean);
                bean.user_id = user_id;
                bean.user_photo = picUpHandl_img.getImgData();
                if(top.ajax.synclBody("/user/edit",bean))
                {
                    modelWindow.alert(top.CMSLang.Add_success);
                    top.getCurrentFrameObj().loadListble();
                    top.tab_colseOnclick();
                }else
                {
                    modelWindow.error(top.CMSLang.Add_fail);
                    is_submit = false;
                }
            }
        }
    }


    function openSelectSinglDiQuPage(title,handl_name)
    {
        modelWindow.openFrameWindow(title,"/sys/cs/project/system/ywdiqu/select_singl_ywdiqu.html?handl_name="+handl_name+'&frame_name='+modelWindow.getFrameWindow(),450,410);
    }

    function setDiQuID(id,name)
    {
        $('#dq_yw_id').val(id);
        $('#dept_name').val(name);
    }

</script>
</body>

</html>
